package edu.xjtu.jih.base;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")// /*表示所有请求都会被本过滤器拦截
public class CrossFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest req=(HttpServletRequest) servletRequest;

        HttpServletResponse resp=(HttpServletResponse) servletResponse;

        String path=req.getServletPath();//获取请求路径

        System.out.println("path:"+path);

        resp.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");//允许所有请求跨域
        resp.setHeader("Access-Control-Max-Age", "3600"); //设置域检过期时间,3600s
        resp.setHeader("Access-Control-Allow-Headers","Origin,X-Requested-With,Content-Type, Accept, Token,Authorization");
        resp.setHeader("Access-Control-Allow-Origin", req.getHeader("origin"));//所有域跨域
        resp.setHeader("Access-Control-Allow-Credentials","true");

        filterChain.doFilter(servletRequest,servletResponse);//通过放行
    }

    @Override
    public void destroy() {

    }
}
